package com.yamaha.bikemarket;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.yamaha.bikemarket.util.LogUtil;
import com.yamaha.bikemarket.util.SCStaticFunc;

public class BrandModelFragment extends AMyActivity {
	private LayoutInflater inflater;

	private final List<Item> list = new ArrayList<Item>();
	private final Set<Integer> selectedModel = new HashSet<Integer>();

	private MyAdapter adapter;

	public BrandModelFragment() {
		super(MenuType.NONE);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.brandmodel_layout);

		inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

		final ImageButton doneButton = (ImageButton) findViewById(R.id.brand_model_done_button);
		doneButton.setOnClickListener(this);

		setListenerForMenu();
	}

	@Override
	protected List<EditText> getEditTexts() {
		return null;
	}

	@Override
	protected void onResume() {
		super.onResume();

		try {
			list.clear();
			selectedModel.clear();
			final JSONArray band = (JSONArray) SCStaticFunc.getData(this,
					SCStaticFunc.BAND);
			for (int i = 0; i < band.length(); i++) {

				final JSONObject b = band.getJSONObject(i);
				final String name = b.getString("name");

				final JSONArray model = b.getJSONArray("model");

				if (model.length() == 0) {
					continue;
				}

				list.add(new HeaderItem(name));

				for (int j = 0; j < model.length(); j++) {
					final JSONObject m = model.getJSONObject(j);

					list.add(new ItemItem(m));
				}
			}

		} catch (JSONException e) {
			LogUtil.e("onStart", e.getMessage(), e);
		}

		adapter = new MyAdapter();
		adapter.setLoading(false);
		final ListView listView = (ListView) findViewById(R.id.b_listview);
		listView.setAdapter(adapter);
	}

	private static class HeaderHolder {
		TextView name;
	}

	private static class ItemHolder {
		CheckBox checkBox;
	}

	private enum ViewType {
		HEADER, ITEM
	}

	private interface Item {
		public int getViewType();

		public View getView(LayoutInflater inflater, View convertView);
	}

	private final class MyAdapter extends BaseAdapter {
		private boolean isLoading = true;

		public void setLoading(boolean isLoading) {
			this.isLoading = isLoading;
		}

		@Override
		public int getViewTypeCount() {
			return ViewType.values().length;
		}

		@Override
		public int getCount() {
			return (isLoading) ? 1 : list.size();
		}

		@Override
		public Object getItem(int position) {
			if (this.isLoading) {
				return null;
			} else {
				return list.get(position);
			}
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public int getItemViewType(int position) {
			if (isLoading) {
				return 1;
			} else {
				return list.get(position).getViewType();
			}
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (isLoading) {
				convertView = inflater.inflate(R.layout.loading_item, parent,
						false);
				convertView.setId(0x0123);
				return convertView;
			}

			if (convertView != null && convertView.getId() == 0x0123) {
				convertView = null;
			}

			return list.get(position).getView(inflater, convertView);
		}
	}

	private class HeaderItem implements Item {

		private final String name;

		public HeaderItem(final String name) {
			this.name = name;
		}

		@Override
		public int getViewType() {
			return ViewType.HEADER.ordinal();
		}

		@Override
		public View getView(LayoutInflater inflater, View convertView) {
			HeaderHolder holder = null;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.band_header_item, null);
				holder = new HeaderHolder();

				holder.name = (TextView) convertView.findViewById(R.id.b_name);

				convertView.setTag(holder);
			} else {
				holder = (HeaderHolder) convertView.getTag();
			}

			holder.name.setText(name);

			return convertView;
		}

	}

	private class ItemItem implements Item {

		private final JSONObject obj;

		public ItemItem(final JSONObject obj) {
			this.obj = obj;
		}

		@Override
		public int getViewType() {
			return ViewType.ITEM.ordinal();
		}

		@Override
		public View getView(LayoutInflater inflater, View convertView) {

			ItemHolder holder = null;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.band_checkbox_item,
						null);
				holder = new ItemHolder();

				holder.checkBox = (CheckBox) convertView
						.findViewById(R.id.checkBox1);

				convertView.setTag(holder);
			} else {
				holder = (ItemHolder) convertView.getTag();
			}

			try {

				final int modelId = obj.getInt("model_id");
				holder.checkBox.setTag(modelId);
				holder.checkBox.setText(obj.getString("name"));
				holder.checkBox.setChecked(selectedModel.contains(modelId));
				holder.checkBox.setOnClickListener(BrandModelFragment.this);

			} catch (JSONException e) {
				LogUtil.e("ItemItem", e.getMessage(), e);
			}

			return convertView;
		}
	}

	@Override
	public void onClick(View v) {

		if (onMenuClick(v)) {
			return;
		}

		switch (v.getId()) {
		case R.id.brand_model_done_button: {

			final Intent intent = new Intent(BrandModelFragment.this,
					MainActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
			intent.putExtra("filterType", "MODEL_LIST");
			final JSONArray array = new JSONArray(selectedModel);
			intent.putExtra("filterParams", array.toString());
			startActivity(intent);

		}
			break;
		case R.id.checkBox1: {
			final CheckBox checkBox = (CheckBox) v;

			final Integer tag = (Integer) checkBox.getTag();
			if (checkBox.isChecked()) {
				selectedModel.add(tag);
			} else {
				selectedModel.remove(tag);
			}
		}
			break;
		}
	}
}
